package com.example.futureai.config.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.futureai.utils.TokenUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName: MyMetaObjectHandler
 * @Description:
 * @Author: YourName
 * @Date: 2025/2/27
 */

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        Object createBy = this.getFieldValByName("createBy", metaObject);
        if(null == createBy){
            this.strictInsertFill(metaObject, "createBy", String.class, TokenUtil.getCurrentUserId());
        }
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        if(null == updateBy){
            this.strictInsertFill(metaObject, "updateBy", String.class, TokenUtil.getCurrentUserId());
        }
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, now);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        if(null == updateBy){
            this.strictInsertFill(metaObject, "updateBy", String.class, TokenUtil.getCurrentUserId());
        }
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
    }
}
